package com.changpay.zywy.help;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import com.changpay.zywy.bean.ShareBean;

import java.util.HashMap;

public enum ShareHelper {
    INSTANCE;
    private Context context ;
    private ShareBean shareBean ;

    //微信好友分享
    public void shareToWeChat(Context context, ShareBean shareBean) {
        this.context = context ;
        this.shareBean = shareBean ;
        if (shareBean == null) {
            return;
        }
        Wechat.ShareParams spWeChat = new Wechat.ShareParams();
        spWeChat.setShareType(Platform.SHARE_WEBPAGE);
        Log.e("mango","shareBean:"+shareBean);
        spWeChat.setText(shareBean.getText());
        spWeChat.setUrl(shareBean.getLinkUrl());
        spWeChat.setTitle(shareBean.getTitle());
        spWeChat.setImageUrl(shareBean.getImageUrl());
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        spWeChat.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        spWeChat.setComment(shareBean.getText());
        Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
        weChat.setPlatformActionListener(new ShareListener()); // 设置分享事件回调
        // 执行图文分享
        weChat.share(spWeChat);

    }

    //朋友圈分享
    public void shareToWeChatFriend(Context context, ShareBean shareBean) {
        this.context = context ;
        this.shareBean = shareBean ;
        if (shareBean == null) {
            return;
        }
        WechatMoments.ShareParams spWeChatMoments = new WechatMoments.ShareParams();
        spWeChatMoments.setShareType(Platform.SHARE_WEBPAGE);
        spWeChatMoments.setText(shareBean.getText());
        spWeChatMoments.setUrl(shareBean.getLinkUrl());
        spWeChatMoments.setImageUrl(shareBean.getImageUrl());
        spWeChatMoments.setTitle(shareBean.getTitle());
        spWeChatMoments.setComment(shareBean.getText());
        Platform weChatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
        weChatMoments.setPlatformActionListener(new ShareListener()); // 设置分享事件回调
        // 执行图文分享
        weChatMoments.share(spWeChatMoments);
    }

//    //QQ分享
//    public void shareToQQ(Context context,ShareBean shareBean) {
//        this.context = context ;
//        this.shareBean = shareBean ;
//        if (shareBean == null) {
//            return;
//        }
//        QQ.ShareParams qqShare = new QQ.ShareParams();
//        qqShare.setShareType(Platform.SHARE_WEBPAGE);
//        qqShare.setTitleUrl(shareBean.getLinkUrl());
//        qqShare.setTitle(shareBean.getTitle());
//        qqShare.setText(shareBean.getText());
//        qqShare.setImageUrl(shareBean.getImageUrl());
//        Platform qqSharePlatFrom = ShareSDK.getPlatform(QQ.NAME);
//        qqSharePlatFrom.setPlatformActionListener(new ShareListener()); // 设置分享事件回调
//        // 执行图文分享
//        qqSharePlatFrom.share(qqShare);
//    }
//
//    //QQ分享--空间
//    public void shareToQQFriend(Context context,ShareBean shareBean) {
//        this.context = context ;
//        this.shareBean = shareBean ;
//        if (shareBean == null) {
//            return;
//        }
//        QZone.ShareParams qqShare = new QZone.ShareParams();
//        qqShare.setShareType(Platform.SHARE_WEBPAGE);
//        qqShare.setTitleUrl(shareBean.getLinkUrl());
//        qqShare.setTitle(shareBean.getTitle());
//        qqShare.setText(shareBean.getText());
//        qqShare.setImageUrl(shareBean.getImageUrl());
//        Platform qqSharePlatFrom = ShareSDK.getPlatform(QZone.NAME);
//        qqSharePlatFrom.setPlatformActionListener(new ShareListener()); // 设置分享事件回调
//        // 执行图文分享
//        qqSharePlatFrom.share(qqShare);
//    }
//
//    //新浪微博分享
//    public void shareToSinaWeibo(ShareBean shareBean){
//        if(shareBean == null){
//            return;
//        }
//        SinaWeibo.ShareParams sina = new SinaWeibo.ShareParams();
//        sina.setShareType(Platform.SHARE_WEBPAGE);
//        sina.setText(shareBean.getText());
//        sina.setUrl(shareBean.getLinkUrl());
//        sina.setTitle(shareBean.getTitle());
//        sina.setImageUrl(shareBean.getImageUrl());
//        Platform sinaShare = ShareSDK.getPlatform(SinaWeibo.NAME);
//        sinaShare.setPlatformActionListener(new ShareListener()); // 设置分享事件回调
//        // 执行图文分享
//        sinaShare.share(sina);
//    }


    class ShareListener implements PlatformActionListener {

        public ShareListener() {
        }
        @Override
        public void onCancel(Platform arg0, int arg1) {
            handler.sendEmptyMessage(0);
        }

        @Override
        public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
            handler.sendEmptyMessage(1);
        }

        @Override
        public void onError(Platform arg0, int arg1, final Throwable arg2) {
            Log.e("gaohua","arg1:"+arg1+",arg2:"+arg2);
            handler.sendEmptyMessage(2);
        }
    }


    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(context,"分享取消",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(context,"分享成功",Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(context,"分享失败，请检查分享内容",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}
